﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StoryboardManager.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Defines the StoryboardManager type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.Silverlight.MVVM.Helpers
{
    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Media.Animation;

    /// <summary>
    /// Callback of the storyboard
    /// </summary>
    /// <param name="state">The state.</param>
    public delegate void Callback(object state);

    /// <summary>
    /// Defines the StoryboardManager type.
    /// </summary>
    public static class StoryboardManager
    {
        /// <summary>
        /// Storyboards dictionary
        /// </summary>
        private static readonly Dictionary<string, Storyboard> Storyboards = new Dictionary<string, Storyboard>();

        /// <summary>
        /// Id dependency property
        /// </summary>
        private static readonly DependencyProperty IDProperty = DependencyProperty.RegisterAttached("ID", typeof(string), typeof(StoryboardManager), new PropertyMetadata(null, IDChanged));

        /// <summary>
        /// Plays the storyboard.
        /// </summary>
        /// <param name="id">The identificator.</param>
        /// <param name="callback">The callback.</param>
        /// <param name="state">The state.</param>
        public static void PlayStoryboard(string id, Callback callback, object state)
        {
            if (!Storyboards.ContainsKey(id))
            {
                callback(state);
                return;
            }

            Storyboard sb = Storyboards[id];
            EventHandler handler = null;
            EventHandler handler1 = handler;
            handler = delegate
                          {
                              sb.Completed -= handler1;
                              callback(state);
                          };
            sb.Completed += handler;
            sb.Begin();
        }

        /// <summary>
        /// Stops the storyboard.
        /// </summary>
        /// <param name="id">The identificator.</param>
        /// <param name="callback">The callback.</param>
        /// <param name="state">The state.</param>
        public static void StopStoryboard(string id, Callback callback, object state)
        {
            if (!Storyboards.ContainsKey(id))
            {
                callback(state);
                return;
            }

            Storyboard sb = Storyboards[id];
            EventHandler handler = null;
            EventHandler handler1 = handler;
            handler = delegate
            {
                sb.Completed -= handler1;
                callback(state);
            };
            sb.Completed += handler;
            sb.Stop();
        }

        /// <summary>
        /// Sets the ID.
        /// </summary>
        /// <param name="obj">The storyboard object.</param>
        /// <param name="id">The stoyyboard id.</param>
        public static void SetID(DependencyObject obj, string id)
        {
            obj.SetValue(IDProperty, id);
        }

        /// <summary>
        /// Gets the ID.
        /// </summary>
        /// <param name="obj">The storyboard object.</param>
        /// <returns>The identificator</returns>
        public static string GetID(DependencyObject obj)
        {
            return obj.GetValue(IDProperty) as string;
        }

        /// <summary>
        /// IDs the changed.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void IDChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var sb = obj as Storyboard;
            if (sb == null)
            {
                return;
            }

            var key = e.NewValue as string;
            if (key != null)
            {
                if (Storyboards.ContainsKey(key))
                {
                    Storyboards[key] = sb;
                }
                else
                {
                    Storyboards.Add(key, sb);
                }
            }
        }
    }
}
